<?php
class CComponent
{
	static function make_component_cache()
	{
		CPortal::$components = array();
		//Khoanb Comment Need optimize file CComponent By Make cache file cache for all xml file 16.10.09
		//Khoanb Edit 28.08.09
		CComponent::get_components(ROOT_DIR.'packages', 'PACKAGE', CPortal::$components);
		CComponent::get_components(ROOT_DIR.'portals','PORTAL', CPortal::$components);
		CComponent::get_components(ROOT_DIR.'pages','PAGE', CPortal::$components);
		CSystem::$cache->set('CPortal::$components',CPortal::$components);
		return CPortal::$components;
	}

	static function get_components($path, $type, &$components, $portal = false)
	{
		$files = CFile($path)->files();
		if($portal)
		{
			$parent_portal = $portal;
		}
		elseif($portal != 'default')
		{
			$parent_portal = 'default';
		}
		else
		{
			$parent_portal = '';
		}
		foreach($files as $file)
		{
			if($type == 'PAGE')
			{
				$name = str_replace('.xml','',$file);
				if($pos = strpos($name, '.'))
				{
					$params = substr($name, $pos + 1);
					$name = substr($name, 0, $pos);
				}
				else
				{
					$params = '';
				}
				if($portal and $portal!='default')
				{
					$params .= ($params?'&':'').'portal='.$portal;
				}
			}
			else
			{
				$name = $file;
			}
			if($type == 'PACKAGE' and !CFile::exists($path.'/'.$file.'/package.xml'))
			{
				continue;
			}
			if($type == 'MODULE' and !CFile::exists($path.'/'.$file.'/module.xml'))
			{
				continue;
			}
			if($type == 'PORTAL' and !CFile::exists($path.'/'.$file.'/portal.xml'))
			{
				continue;
			}
			if($type == 'PAGE' and !strpos($file,'.xml'))
			{
				continue;
			}
			//echo substr($path, strlen(ROOT_DIR)).'<br/>';
			$components[$type.'.'.(($type=='PACKAGE')?$path.'/'.$file:$name).(($type == 'PAGE' and $params)?'.'.$params:'')] = array(
				'name' => $name,
				//Khoanb Edit 28.08.09
				//'path' => $path.'/'.$file,
				'path' => substr($path, strlen(ROOT_DIR)).'/'.$file,
				'type' => $type,
			)
			+ (($type == 'PAGE')? array('params' => $params, 'portal'=>$portal):array())
			+ (($type == 'PORTAL')? array('parent' => ($parent_portal!=$name)?$parent_portal:''):array())
			+ (($type == 'MODULE')? array('parent' => str_replace('/modules','',$path)):array());
			if($type == 'PACKAGE')
			{
				CComponent::get_components($path.'/'.$file.'/modules', 'MODULE', $components, $portal);
				CComponent::get_components($path.'/'.$file.'/packages', 'PACKAGE', $components, $portal);
				CComponent::get_components($path.'/'.$file.'/pages', 'PAGE', $components, $portal);
			}
			elseif($type == 'PORTAL')
			{
				CComponent::get_components($path.'/'.$file.'/modules', 'MODULE', $components, $file);
				CComponent::get_components($path.'/'.$file.'/packages', 'PACKAGE', $components, $file);
				CComponent::get_components($path.'/'.$file.'/portals', 'PORTAL', $components, $file);
				CComponent::get_components($path.'/'.$file.'/pages', 'PAGE', $components, $file);
			}
			elseif($type == 'MODULE')
			{
				CComponent::get_components($path.'/'.$file.'/pages', 'PAGE', $components, $portal);
			}
		}
	}
}
?>